package com.cn.wanxi.servlet;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//req 前端页面请求后台
//        resp  后台响应给前端
//        拿到数据并把数据转发到jsp页面？
//        页面转发,重定向?
//        servlet 四大作用域？
//        page  当前页面有效
//        request 只有跳转的页面有效
//        session 只要会话期有效，不能跨浏览器
//        application 整个项目应用程序有效 可以跨浏览器访问
//        目的？作用？
//        都是存储数据的容器，区别在于什么周期和生效范围

//        CompanyService companyService = new CompanyService();
//        CompanyModel companyModel = companyService.getCompanyModel();
//        //        null  companyModel  区别？
////      companyModel=  null 表示对象为空
////    companyModel=     new CompanyModel();对象里面的值为null
//        req.setAttribute("companyModel", companyModel);
////        req.setAttribute("logo", "028-9999999");
////        req.setAttribute("phone", "028-9999999");//key-value的形式经常存储
//
////        返回导航数据
//        List<NavModel> navModelList = new ArrayList<>();
//        NavService navService = new NavService();
//        navModelList = navService.getNavModelList();
//        req.setAttribute("navModelList", navModelList);
        CompanyAndNavServlet.setCompanyAndNavData(req);
//        返回新闻数据
        NewsService newsService = new NewsService();
        List<NewsModel> newsModelList = newsService.getNewsModelListForHomePage();
        req.setAttribute("newsModelList", newsModelList);
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
//        resp.sendRedirect("/jsp/home.jsp");//重定向
    }
}
